import java.util.List;

/*
 * if sufficient capacity is not available even from multiple providers, put
 * the tour offering on hold and release any reservations already made. if on
 * the departure date, sufficient availability for each event exists for the
 * actual party size, reschedule the offering.
 */
public class SchedulerB implements Scheduler
{
    private String message;
    
    public boolean schedule(TourOffering offering)
    {
        offering.hold();

        String cancels = "";
        List<Reservation> cancellations = offering.applyCancellations();
        
        // add reservation cancellations to message to communicate to user
        // that cancellations were made
        for (Reservation r : cancellations)
        {
            cancels += r.getTravelerName() + " cancelled.\n";
        }
            
        // check that tour isn't over-filled
        if (offering.getNumReservations() <= offering.getTour().getMaxSize())
        {
            int eventCapacity;
            
            // check events' provider + aux providers have enough room
            for (Event e : offering.getTour().getEvents())
            {
                eventCapacity = 0;
                
                // go through all providers and find total capacity
                for (Provider p : e.getProviders())
                {
                    eventCapacity += p.getCapacity();
                }
                
                // is there enough room for everyone in the event?
                if (e.getProvider() != null
                        && ((e.getEventType() == EventType.LODGING && eventCapacity < offering.getRoomsRequired()) || (e.getEventType() != EventType.LODGING && eventCapacity < offering
                                .getTour().getMaxSize())))
                {
                    message =  cancels + e.getEventType() + " event over capacity even with multiple providers.";
                    return false;
                }
            }
        }
        else
        {
            message = cancels + "Tour over capacity";
            return false;
        }
        
        offering.offHold();
        message = cancels + "After cancellations, tour can run.";
        return true;
    }
    
    public String getMessage()
    {
        return "Level B: " + message;
    }
}
